// pages/choice/choice.js
Component({
  data: {
    randomPhoto: '',
    vip: '隆江猪脚饭',
    menu: [{
        isSelect: false,
        id: '北京烤鸭',
        jmg: 'https://img0.baidu.com/it/u=3473745404,3694229320&fm=253&fmt=auto&app=138&f=JPEG?w=554&h=374'
      },
      {
        isSelect: false,
        id: '肠粉',
        jmg: 'https://img2.baidu.com/it/u=2043862180,3457540087&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=480'
      },
      {
        isSelect: false,
        id: '虾饺',
        jmg: 'http://t15.baidu.com/it/u=4224617265,3841188360&fm=224&app=112&f=JPEG?w=500&h=500'
      },
      {
        isSelect: false,
        id: '扣肉',
        jmg: 'https://img0.baidu.com/it/u=4089682555,4049987245&fm=253&fmt=auto&app=138&f=JPEG?w=600&h=457'
      },
      {
        isSelect: false,
        id: '鲍鱼',
        jmg: 'https://img0.baidu.com/it/u=260728631,1257856948&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'
      },
      {
        isSelect: false,
        id: '烤乳猪',
        jmg: 'https://img2.baidu.com/it/u=3781358659,3824741889&fm=253&fmt=auto&app=138&f=PNG?w=799&h=450'
      },
      {
        isSelect: false,
        id: '梅菜扣肉',
        jmg: 'https://img1.baidu.com/it/u=4052801268,1156067884&fm=253&fmt=auto&app=138&f=JPEG?w=891&h=500'
      },
      {
        isSelect: false,
        id: '粉红猪肉卷',
        jmg: 'https://img1.baidu.com/it/u=1875312581,1484426144&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=332'
      },
      {
        isSelect: false,
        id: '白切鸡',
        jmg: 'https://img1.baidu.com/it/u=2535984388,3211144124&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'
      },
    ],


  },

  methods: {
    random: function () {
      //清除上一次选中
      for (var x = 0; x < this.data.menu.length; x++) {
        this.data.menu[x].isSelect = false
        this.setData({
          menu: this.data.menu
        })
      }

      //获取随机数
      var y = Math.floor(Math.random() * 18)

      //从0开始，将已选中的isSelect改为false
      var x1 = 0
      var x2 = 0
      this.data.randomPhoto = setInterval(() => {
        setTimeout(() => {
          this.data.menu[x1].isSelect = false
          this.setData({
            menu: this.data.menu
          })
          x1 = (x1 + 1) % this.data.menu.length
          x2++
        }, 200)

        //从0开始，将未选中的isSelect改为true
        this.data.menu[x1].isSelect = true
        this.setData({
          vip: this.data.menu[x1].id,
          menu: this.data.menu
        })

        // 清除循环（停止动态出口）
        if (x2 > y) {
          clearInterval(this.data.randomPhoto)
          setTimeout(() => {
            this.data.menu[x1-1].isSelect = true
            this.setData({
              menu: this.data.menu
            })
          }, 500)
        }
      }, 350);
      
    },
  },

  observers: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

})